package com.task.mall.dao.impl;

import com.alibaba.druid.util.StringUtils;
import com.task.mall.bean.Admin;
import com.task.mall.bean.Login;
import com.task.mall.bean.Password;
import com.task.mall.dao.AdminDao;
import com.task.mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AdminDaoImpl implements
        AdminDao {
    /**
     * 搭建runner
     */
    QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());

    @Override
    public int login(Admin admin) {
        Admin query = null;
        try {
            query = runner.query("select * from admin where email = ? and pwd = ?",
                    new BeanHandler<>(Admin.class),
                    admin.getEmail(),
                    admin.getPwd());
        } catch (SQLException e) {
            e.printStackTrace();
            return 500;
        }
        if (query != null) {
            return 200;
        }
        return 404;
    }

    @Override
    public List<Admin> queryAllAdmins() {
        List<Admin> adminList = null;
        try {
            adminList = runner.query("select * from admin",
                    new BeanListHandler<>(Admin.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return adminList;
    }

    /**
     * 动态地去拼接sql
     * 动态sql的核心：where 1 = 1
     * object... 相当于  object[]
     */
    @Override
    public List<Admin> querySearchAdmins(Admin admin) {
        // 首先写出基本的sql
        String baseSql = "select * from admin where 1 = 1 ";
        List<Object> params = new ArrayList<>();
        if (!StringUtils.isEmpty(admin.getEmail())){
            baseSql = baseSql + " and email like ?";
            // 模糊查询email账号
            params.add("%"+admin.getEmail()+"%");
        }
        if (!StringUtils.isEmpty(admin.getNickname())){
            baseSql = baseSql + " and nickname like ?";
            // 模糊查询nickname用户名
            params.add("%"+admin.getNickname()+"%");
        }

        // 检验输出的sql
        // System.out.println(baseSql);
        List<Admin> adminList = null;
        try {
            adminList = runner.query(baseSql, new BeanListHandler<Admin>(Admin.class),
                    params.toArray());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return adminList;
    }

    @Override
    public int addAdminss(Admin admin) {
        int update = 0;
        try {
            update = runner.update("insert into admin values (null, ?, ?, ?)",
                    admin.getEmail(),
                    admin.getNickname(),
                    admin.getPwd());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (update != 0) {
            return 200;
        }
        return 404;
    }

    @Override
    public int deleteAdmins(int id) {
        try {
            runner.update("delete from admin where id = ?", id);
            return 200;
        } catch (SQLException e) {
            e.printStackTrace();
        }
       return 10000;
    }

    @Override
    public Admin getAdminsInfo(int id) {
        Admin query = null;
        try {
            query = runner.query("select * from admin where id = ?",
                    new BeanHandler<>(Admin.class),
                    id);
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateAdminss(Admin admin) {
        try {
            runner.update("update admin set email = ?, nickname = ?, pwd = ? where id = ?",
                    admin.getEmail(),
                    admin.getNickname(),
                    admin.getPwd(),
                    admin.getId());
            return 200;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 10000;
    }

    @Override
    public int changePwd(Password password) {
        Admin query = null;
        try {
            query = runner.query("select * from admin where nickname = ?",
                    new BeanHandler<>(Admin.class),
                    password.getAdminToken());
            if (!(password.getOldPwd().equals(query.getPwd()))){return 201;}
            if (!(password.getNewPwd().equals(password.getConfirmPwd()))){return 202;}
            runner.update("update admin set pwd = ? where nickname = ? and pwd = ?",
                    password.getConfirmPwd(),
                    password.getAdminToken(),
                    password.getOldPwd());
            return 200;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 404;
    }

    @Override
    public Login getLoginInfo(String email) {
        Login loginInfo = null;
        try {
            loginInfo = runner.query("select nickName from admin where email = ?",
                    new BeanHandler<>(Login.class),
                    email);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return loginInfo;
    }
}
